//
//  SEGReportViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/16.
//

#import "SEGReportViewModel.h"

@implementation SEGReportViewModel
- (instancetype)init {
    if (self = [super init]) {
        [self initData];
        [self seg_creatDataCommand];
        [self seg_creatReportCommand];
    }
    return self;
}

- (void)initData {
    self.dataSource = [NSArray array];
    self.selectedIndex = -1;
}

- (void)seg_creatDataCommand {
    @weakify(self);
    self.dataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            [self.sharedNewManager requestReportReasonType:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                
                if (isSuccessed) {
                    self.dataSource = self.sharedNewManager.reportReasonArray;
                    [subscriber sendNext:nil];
                }else {
                    [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                }
                [subscriber sendCompleted];
            }];
            return nil;
        }];
    }];
}

- (void)seg_creatReportCommand {
    @weakify(self);
    self.reportCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            if (self.selectedIndex == -1) {
                [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:200 userInfo:@{NSLocalizedDescriptionKey:@""}]];
                return nil;
            }
            [subscriber sendNext:@(1)];
            [self.sharedNewManager requestSubmitComplaintWithByReportedId:self.byReportedId byReportedNickname:self.byReportedNickname byReportedContId:self.byReportedContId byReportedContTypeCode:self.byReportedContTypeCode byReportedContDetails:self.byReportedContDetails byReportedContImages:self.byReportedContImages reportReasonCode:self.reportReasonCode reportReasonName:self.reportReasonName pgcTitle:self.pgcTitle finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
                [subscriber sendNext:@(0)];
                if (isSuccessed) {
                    [subscriber sendCompleted];
                } else {
                    [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                }
            }];            
            return nil;
        }];
    }];
}

- (SEGSharedNewManager *)sharedNewManager
{
    if (!_sharedNewManager) {
        _sharedNewManager = [[SEGSharedNewManager alloc] init];
    }
    return _sharedNewManager;
}

- (NSString *)byReportedContImages
{
    if (!_byReportedContImages) {
        _byReportedContImages = @"";
    }
    return _byReportedContImages;
}

@end
